package com.ttdevs.app;

import android.app.ActivityManager;
import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * 
 * 应用的初始化：主要包括Volley的初始化<br>
 * 参考：https://github.com/ogrebgr
 * 
 * @author ttdevs 2014年2月24日
 */
public class AppHttp {

	private static RequestQueue mRequestQueue;
	private static ImageLoader mImageLoader;
	private static BitmapLruCache mBitmCache;

	private AppHttp() {

	}

	static void init(Context context) {
		mRequestQueue = Volley.newRequestQueue(context);

		int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
		int cacheSize = 1024 * 1024 * memClass / 8; // 使用1/8内存用来缓存图片
		VolleyLog.d("%s:%d %s", "ImageCache size：", cacheSize / 1024 / 1024, "MB");
		mBitmCache = new BitmapLruCache(cacheSize, true);
		mImageLoader = new ImageLoader(mRequestQueue, mBitmCache);
	}

	public static RequestQueue getRequestQueue() {
		if (mRequestQueue != null) {
			return mRequestQueue;
		} else {
			throw new IllegalStateException("AppInit：RequestQueue not initialized");
		}
	}

	public static ImageLoader getImageLoader() {
		if (mImageLoader != null) {
			return mImageLoader;
		} else {
			throw new IllegalStateException("AppInit：ImageLoader not initialized");
		}
	}

	public static void stopImageLoaderCache() {
		if (mBitmCache != null) {
			mBitmCache.quit();
		}
	}
}
